package jnpf.database.source.impl;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.handler.TableNameHandler;
import jnpf.database.data.DataSourceContextHolder;
import jnpf.database.enums.DbAliasEnum;
import jnpf.database.enums.DbSttEnum;
import jnpf.database.enums.datatype.DataTypeEnum;
import jnpf.database.enums.datatype.DtKingbaseEnum;
import jnpf.database.model.DataTypeModel;
import jnpf.database.model.DbTableFieldModel;
import jnpf.database.model.DbTableModel;
import jnpf.database.source.DbBase;
import jnpf.database.sql.SqlBase;
import jnpf.database.sql.impl.SqlKingbase;
import jnpf.database.util.CommonUtil;
import jnpf.database.util.DataSourceUtil;
import jnpf.exception.DataException;
import jnpf.util.StringUtil;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;

/**
 * 金仓模型
 *
 * @author JNPF开发平台组 YY
 * @version V3.2.0
 * @copyright 引迈信息技术有限公司
 * @date 2021/10/06
 */
public class DbKingbase extends DbBase {

    public static final String DB_ENCODE = "KingbaseES";
    public static final String DATA_SUM_SQL = "SELECT count(*) AS COUNT_SUM FROM "+ DbSttEnum.TABLE.getTarget();
    public static final String CONN_URL = "jdbc:kingbase8://{host}:{port}/{dbname}";


    @Override
    public void setDbType() {
        mpDbType = DbType.KINGBASE_ES;
        connUrlEncode = "kingbase8";
        dbEncode = DB_ENCODE;
        driver = "com.kingbase8.Driver";
    }

    @Override
    public SqlBase getSqlBase() {
        return new SqlKingbase(this);
    }

    @Override
    public DataTypeModel getDataTypeModel(DataTypeEnum dte){
        return dte.getDtKingbaseEnum().getDataTypeModel();
    }

    @Override
    public DataTypeModel getDataTypeModel(String dbFieldType) {
        for(DtKingbaseEnum dtKingbaseEnum : DtKingbaseEnum.values()){
            if(dbFieldType.equals(dtKingbaseEnum.getDbFieldType())){
                return dtKingbaseEnum.getDataTypeModel();
            }
        }
        return null;
    }

    @Override
    public String getConnUrl(String prepareUrl, String host, Integer port, String dbName) {
        if (StringUtil.isEmpty(prepareUrl)) {
            prepareUrl = CONN_URL;
        }
        return super.getConnUrl(prepareUrl, host, port, dbName);
    }

    @Override
    public void setDynamicMap(HashMap<String, TableNameHandler> map, DbTableModel dbTableModel) {
        map.put(dbTableModel.getTable(), (sql, tableName) -> DataSourceContextHolder.getDatasourceName()+"."+dbTableModel.getTable());
    }

    @Override
    public DbTableFieldModel getFieldModel(ResultSet result) throws SQLException, DataException {
        DbTableFieldModel model = new DbTableFieldModel();
        //主键
        if(result.getString(DbAliasEnum.PRIMARY_KEY.AS()) != null){
            model.setPrimaryKey(DbAliasEnum.PRIMARY_KEY.isTrue());
        }else {
            model.setPrimaryKey(DbAliasEnum.PRIMARY_KEY.isFalse());
        }
        //允空
        if(result.getBoolean(DbAliasEnum.ALLOW_NULL.AS())){
            model.setAllowNull(DbAliasEnum.ALLOW_NULL.isFalse());
        }else {
            model.setAllowNull(DbAliasEnum.ALLOW_NULL.isTrue());
        }
        return model;
    }


    @Override
    public String getDbTime(Connection conn) {
        return null;
    }

    @Override
    public String getReplaceSql(String sql, String table, DataSourceUtil dbSourceOrDbLink) {
        /**
         * 说明：一个实例对应多个数据库，一个数据库对应多个模式，一个用户对应一个模式
         * 数据库 = dbName;
         * 模式 = schema = userName;
         * 表空间需默认：JNPF
         */
        dbSourceOrDbLink.setDbSchema(dbSourceOrDbLink.getUserName());
        return CommonUtil.getReplaceSql(sql,table,dbSourceOrDbLink,
                true,
                true,
                true);
    }

}
